<?php
/**
 * prediction.php
 * 
 * Written by: A. Sari
 * $Id: result.php 9 2009-07-15 07:14:04Z ahmtsari $
 */
class Prediction {
	var $id;
	var $username;
	var $home_score;
	var $away_score;
	
	function Prediction($id = 0) {
		global $session;
		$this->id = $id;
		//get game & pred data
		$this->username = $session->username; 
	}
	
	function setPrediction($game_id) {
		global $database;
		$q = "SELECT * FROM predictions WHERE username='$this->username' AND game_id=$game_id";
		foreach ($database->cursor($q) as $row)	{
			$this->id = $row["id"];
			$this->home_score = $row["home_score"];
			$this->away_score = $row["away_score"];
			break;
		}
	}
	
	function upsert($game_id) {
		global $database;
		$this->id = $id;
		if (isset($this->id)) {
			$q = "UPDATE predictions";
			$q .= " SET home_score = $this->home_score, away_score=$this->away_score";
			$q .= " WHERE id=$this->id;";
		}
		else {
			$q = "INSERT INTO predictions (username, game_id, home_score, away_score)";
			$q .= " VALUES ('$this->username', $game_id, $this->home_score, $this->away_score);";
		}
		echo $q;
		return $database->query($q);		
	}	
}
?>